package com.example.springboot_zc.controller;

import com.example.springboot_zc.pojo.*;
import com.example.springboot_zc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/self")
public class selfController {

    @Autowired
    dydataService dydataService;
    @Autowired
    zydataService zydataService;
    @Autowired
    lydataService lydataService;
    @Autowired
    mydataService mydataService;
    @Autowired
    tydataService tydataService;
    @Autowired
    classService classService;


    @GetMapping("/selectDyData/{stuNum}")
    public List<DyData> findAllDyData(@PathVariable("stuNum") String stuNum) {
        return dydataService.selectByStu(stuNum);
    }

    @GetMapping("/selectZyData/{stuNum}")
    public List<ZyData> findAllZyData(@PathVariable("stuNum") String stuNum) {
        return zydataService.selectByStu(stuNum);
    }

    @GetMapping("/selectMyData/{stuNum}")
    public List<MyData> findAllMyData(@PathVariable("stuNum") String stuNum) {
        return mydataService.selectByStu(stuNum);
    }

    @GetMapping("/selectLyData/{stuNum}")
    public List<LyData> findAllLyData(@PathVariable("stuNum") String stuNum) {
        return lydataService.selectByStu(stuNum);
    }

    @GetMapping("/selectTyData/{stuNum}")
    public List<TyData> findAllTyData(@PathVariable("stuNum") String stuNum) {
        return tydataService.selectByStu(stuNum);
    }

    @GetMapping("/save")
    public String saveData() {

        return ("ZyData:" + zydataService.listMaps() + ",DyData:" + dydataService.listMaps()
                + ",TyData:" + tydataService.listMaps() + ",MyData:" + mydataService.listMaps()
                + ",LyData:" + lydataService.listMaps()).replace("=", ":");

    }


    @PostMapping("/add")
    public void sp(@RequestBody Object o) {

        String s = o.toString();
//        System.out.println(s);
        String[] str = s.split("\\{");
        int flag1 = -1, flag2 = -1, i = 0;
        int[] num = {0, 0, 0, 0, 0};
        for (String q : str) {
            if (flag1 < 1) {
                flag1++;
                continue;
            }
//            System.out.println(q);
            int y = q.indexOf('}');
            int a = q.substring(y + 1, q.length() - 1).indexOf("Data");
            num[i]++;
            if (a != -1) {
                i++;
            }
        }
        for (int j = 0; j < num.length; j++) {
            num[j] = num[j] * 10;
//            System.out.println(num[j]);
        }
        int zin = 0, din = 0, tin = 0, min = 0, lin = 0;
        Boolean Zf=true,Df=true,Tf=true,Mf=true,Lf=true;
        for (String q : str) {
            if (flag2 < 1) {
                flag2++;
                continue;
            }
            int y = q.indexOf('}');
            String p = q.substring(0, y + 1);
            p = '{' + p;
//            System.out.println(p);}
            Map<String, String> m = selfController.mapStringToMap(p);
            Set<String> keys = m.keySet();
//            for(String key:keys){
//                System.out.println("key: "+key+" value: "+m.get(key));
//            }
            ZyData zyData = new ZyData();
            DyData dyData = new DyData();
            TyData tyData = new TyData();
            MyData myData = new MyData();
            LyData lyData = new LyData();

            for (String key : keys) {
//                System.out.println("key："+key+" value:"+m.get(key));
                if (num[0] > 0) {
                    if (key.equals("eviMat"))
                        zyData.setEviMat(m.get(key));
                    if (key.equals("zyNewTime"))
                        zyData.setZyNewTime(m.get(key));
                    if (key.equals("zyBwScore"))
                        zyData.setZyBwScore(Integer.valueOf(m.get(key)));
                    if (key.equals("xxName"))
                        zyData.setXxName(m.get(key));
                    if (key.equals("sugScore"))
                        zyData.setSugScore(Integer.valueOf(m.get(key)));
                    if (key.equals("zySelfScore"))
                        zyData.setZySelfScore(Integer.valueOf(m.get(key)));
                    if (key.equals("contentSm"))
                        zyData.setContentSm(m.get(key));
                    if(key.equals("stuNum")) {
                        zyData.setStuNum(m.get(key));
                        if(Zf==true) {
                            zydataService.deleteByStu(m.get(key));
                            Zf=false;
                        }
                    }
                    if(key.equals("classNum"))
                        zyData.setClassNum(m.get(key));
                    num[0]--;
                    if (num[0] % 10 == 0) {
                        zyData.setId(zin);
//                        System.out.println(zyData);
                        zin++;
                        zydataService.save(zyData);
                    }
                    continue;
                }

                if (num[1] > 0) {
                    if (key.equals("eviMat"))
                        dyData.setEviMat(m.get(key));
                    if (key.equals("dyNewTime"))
                        dyData.setDyNewTime(m.get(key));
                    if (key.equals("dyBwScore"))
                        dyData.setDyBwScore(Integer.valueOf(m.get(key)));
                    if (key.equals("xxName"))
                        dyData.setXxName(m.get(key));
                    if (key.equals("sugScore"))
                        dyData.setSugScore(Integer.valueOf(m.get(key)));
                    if (key.equals("dySelfScore"))
                        dyData.setDySelfScore(Integer.valueOf(m.get(key)));
                    if (key.equals("contentSm"))
                        dyData.setContentSm(m.get(key));
                    if(key.equals("stuNum")) {
                        dyData.setStuNum(m.get(key));
                        if(Df==true){
                            dydataService.deleteByStu(m.get(key));
                            Df=false;
                        }
                    }
                    if(key.equals("classNum"))
                        dyData.setClassNum(m.get(key));
                    num[1]--;
                    if (num[1] % 10 == 0) {
                        dyData.setId(din);
//                        System.out.println(dyData);
                        din++;
                        dydataService.save(dyData);
                    }
                    continue;
                }

                if (num[2] > 0) {
                    if (key.equals("eviMat"))
                        tyData.setEviMat(m.get(key));
                    if (key.equals("tyNewTime"))
                        tyData.setTyNewTime(m.get(key));
                    if (key.equals("tyBwScore"))
                        tyData.setTyBwScore(Integer.valueOf(m.get(key)));
                    if (key.equals("xxName"))
                        tyData.setXxName(m.get(key));
                    if (key.equals("sugScore"))
                        tyData.setSugScore(Integer.valueOf(m.get(key)));
                    if (key.equals("tySelfScore"))
                        tyData.setTySelfScore(Integer.valueOf(m.get(key)));
                    if (key.equals("contentSm"))
                        tyData.setContentSm(m.get(key));
                    if(key.equals("stuNum")) {
                        tyData.setStuNum(m.get(key));
                        if(Tf==true){
                            tydataService.deleteByStu(m.get(key));
                            Tf=false;
                        }
                    }
                    if(key.equals("classNum"))
                        tyData.setClassNum(m.get(key));
                    num[2]--;
                    if (num[2] % 10 == 0) {
                        tyData.setId(tin);
//                        System.out.println(tyData);
                        tin++;
                        tydataService.save(tyData);
                    }
                    continue;
                }

                if (num[3] > 0) {
                    if (key.equals("eviMat"))
                        lyData.setEviMat(m.get(key));
                    if (key.equals("lyNewTime"))
                        lyData.setLyNewTime(m.get(key));
                    if (key.equals("lyBwScore"))
                        lyData.setLyBwScore(Integer.valueOf(m.get(key)));
                    if (key.equals("xxName"))
                        lyData.setXxName(m.get(key));
                    if (key.equals("sugScore"))
                        lyData.setSugScore(Integer.valueOf(m.get(key)));
                    if (key.equals("lySelfScore"))
                        lyData.setLySelfScore(Integer.valueOf(m.get(key)));
                    if (key.equals("contentSm"))
                        lyData.setContentSm(m.get(key));
                    if(key.equals("stuNum")) {
                        lyData.setStuNum(m.get(key));
                        if(Lf==true){
                            lydataService.deleteByStu(m.get(key));
                            Lf=false;
                        }
                    }
                    if(key.equals("classNum"))
                        lyData.setClassNum(m.get(key));
                    num[3]--;
                    if (num[3] % 10 == 0) {
                        lyData.setId(lin);
//                        System.out.println(lyData);
                        lin++;
                        lydataService.save(lyData);
                    }
                    continue;
                }

                if (num[4] > 0) {
                    if (key.equals("eviMat"))
                        myData.setEviMat(m.get(key));
                    if (key.equals("myNewTime"))
                        myData.setMyNewTime(m.get(key));
                    if (key.equals("myBwScore"))
                        myData.setMyBwScore(Integer.valueOf(m.get(key)));
                    if (key.equals("xxName"))
                        myData.setXxName(m.get(key));
                    if (key.equals("sugScore"))
                        myData.setSugScore(Integer.valueOf(m.get(key)));
                    if (key.equals("mySelfScore"))
                        myData.setMySelfScore(Integer.valueOf(m.get(key)));
                    if (key.equals("contentSm"))
                        myData.setContentSm(m.get(key));
                    if(key.equals("stuNum")) {
                        myData.setStuNum(m.get(key));
                        if(Mf==true){
                            mydataService.deleteByStu(m.get(key));
                            Mf=false;
                        }
                    }
                    if(key.equals("classNum"))
                        myData.setClassNum(m.get(key));
                    num[4]--;
                    if (num[4] % 10 == 0) {
                        myData.setId(min);
//                        System.out.println(myData);
                        min++;
                        mydataService.save(myData);
                    }
                    continue;

                }
            }

        }
        //提交到class表

    }


    public static Map<String, String> mapStringToMap(String str) {
        str = str.substring(1, str.length() - 1);
        String[] strs = str.split(",");
        Map<String, String> map = new HashMap<String, String>();
        for (String string : strs) {
            String key = string.split("=")[0];
            String value = string.split("=")[1];
            // 去掉头部空格
            String key1 = key.trim();
            String value1 = value.trim();
            map.put(key1, value1);
        }
        return map;
    }

}
